package com.antgroup.seckill.business.common.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.antgroup.seckill.business.property.AlipayProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangxy
 * @date 2024/10/23 22:59
 */
@Configuration
public class AlipayClientConfig {

    @Autowired
    public AlipayProperty alipayProperty;

    @Bean
    public AlipayClient aipayClient() {
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
            return alipayClient;
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

    }

    private AlipayConfig getAlipayConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setPrivateKey(alipayProperty.getPrivateKey());
        alipayConfig.setFormat(alipayProperty.getFormat());
        alipayConfig.setAlipayPublicKey(alipayProperty.getAlipayPublicKey());
        alipayConfig.setCharset(alipayProperty.getCharset());
        alipayConfig.setSignType(alipayProperty.getSignType());
        alipayConfig.setServerUrl(alipayProperty.getServerUrl());
        return alipayConfig;
    }
}
